package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.entity.SpecialEquipmentAccount;
import com.ruoyi.system.entity.SysRemind;
import com.ruoyi.system.mapper.SpecialEquipmentAccountMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.ViewConfigPushMesMapper;
import com.ruoyi.system.service.SpecialEquipmentAccountService;
import com.ruoyi.system.service.SysRemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 特种设备台账(SpecialEquipmentAccount)表服务实现类
 *
 * @author makejava
 * @since 2023-03-20 16:41:22
 */
@Service("specialEquipmentAccountService")
public class SpecialEquipmentAccountServiceImpl implements SpecialEquipmentAccountService {
    @Autowired
    private SysRemindService sysRemindService;
    @Resource
    private EnterpriseWXServiceImpl enterpriseWXService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SpecialEquipmentAccountMapper specialEquipmentAccountDao;

    @Resource
    private ViewConfigPushMesMapper viewConfigPushMesMapper;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SpecialEquipmentAccount queryById(Integer id) {
        return this.specialEquipmentAccountDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param specialEquipmentAccount 筛选条件
     * @param pageRequest             分页对象
     * @return 查询结果
     */
    @Override
    public Page<SpecialEquipmentAccount> queryByPage(SpecialEquipmentAccount specialEquipmentAccount, PageRequest pageRequest) {
        long total = this.specialEquipmentAccountDao.count(specialEquipmentAccount);
        return new PageImpl<>(this.specialEquipmentAccountDao.queryAllByLimit(specialEquipmentAccount, pageRequest), pageRequest, total);
    }

    @Override
    public List<SpecialEquipmentAccount> queryAll(SpecialEquipmentAccount specialEquipmentAccount) {
        return specialEquipmentAccountDao.queryAll(specialEquipmentAccount);
    }

    /**
     * 新增数据
     *
     * @param specialEquipmentAccount 实例对象
     * @return 实例对象
     */
    @Override
    public SpecialEquipmentAccount insert(SpecialEquipmentAccount specialEquipmentAccount) {
        this.specialEquipmentAccountDao.insert(specialEquipmentAccount);
        return specialEquipmentAccount;
    }

    /**
     * 修改数据
     *
     * @param specialEquipmentAccount 实例对象
     * @return 实例对象
     */
    @Override
    public SpecialEquipmentAccount update(SpecialEquipmentAccount specialEquipmentAccount) {
        this.specialEquipmentAccountDao.update(specialEquipmentAccount);
        return this.queryById(specialEquipmentAccount.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.specialEquipmentAccountDao.deleteById(id) > 0;
    }


    @Scheduled(cron = "0 0 6 * * ?")
    public void checkPermitReportFile() {
        SpecialEquipmentAccount specialEquipmentAccount = new SpecialEquipmentAccount();
        List<SpecialEquipmentAccount> relateds = specialEquipmentAccountDao.queryAll(specialEquipmentAccount);
        if (CollectionUtils.isEmpty(relateds)) {
            return;
        }

        relateds.forEach(var -> {
            String remind = DateUtils.parseDateToStr("yyyy-MM-dd", var.getNextCheckDate());
            Date date = new Date();

            Date parseDate = DateUtils.parseDate(remind);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(parseDate);
            // 把日期往后增加一天,整数  往后推,负数往前移动
            calendar.add(Calendar.DATE, -1);

            boolean after = date.after(calendar.getTime());
            if (after) {
                String stringBuffer =
                        "设备注册代码:" + var.getRegistrationCode() +
                                "即将到期。";

                SysRole sysRole = new SysRole();
                sysRole.setRoleKey("qyzzts");
                List<SysUser> sysUsers = sysUserMapper.selectUserByRoleName(sysRole);
                if (CollectionUtils.isEmpty(sysUsers)) {
                    return;
                }

                for (SysUser sys : sysUsers) {
                    sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, sys.getUserId().intValue());
                }

                List<String> collect = sysUsers.stream().map(SysUser::getWxUserId).collect(Collectors.toList());
                EnterpriseWXServiceImpl.SendMessageRequest sendMessageRequest = new EnterpriseWXServiceImpl.SendMessageRequest();
                sendMessageRequest.setTouser(CollUtil.join(collect, "|"));
                sendMessageRequest.setMsgtype("text");
                sendMessageRequest.setAgentid(1000319);
                EnterpriseWXServiceImpl.MessageContent messageContent = new EnterpriseWXServiceImpl.MessageContent();
                messageContent.setContent(stringBuffer);
                sendMessageRequest.setText(messageContent);
                enterpriseWXService.pushMessage(sendMessageRequest);

            }
        });
    }


}
